package com.dormitory.management.system.utils;


import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * @author zhangchengming
 * @remake 邮件发送工具类( 需要发送人的邮箱开启SMTP服务，例：163开启： 登陆网页版 ---》设置--》设置POP3/SMTP/IMAP   勾选就行)
 * @date 2021/11/26
 */
public class EmailUtil {
    private static final String HOST = "smtp.qq.com"; // 邮箱服务器
    private static final Integer PORT = 25;	// 端口
    private static  String USERNAME;    //邮箱名
    private static  String PASSWORD;	//密码
    private static  String EMAILFORM;  //发件人(需要与邮箱名一致，不然会出现553问题)

    /**
     * 邮件发送器
     * @return 配置好的工具
     */
    public static JavaMailSenderImpl createMailSender(String USERNAME,String PASSWORD) {
        EmailUtil.USERNAME=USERNAME;
        EmailUtil.PASSWORD=PASSWORD;
        EmailUtil.EMAILFORM=EmailUtil.USERNAME;
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setPort(PORT);
        sender.setUsername(USERNAME);
        sender.setPassword(PASSWORD);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", "25000");
        p.setProperty("mail.smtp.auth", "true");
        sender.setJavaMailProperties(p);
        return sender;
    }

    /**
     * 发送邮件
     * @remake 基础模板 String str="同学你好，<br/>恭喜通过DXC后端实习岗位技术一面，想跟你约一下技术二面的时间，麻烦加一下我的微信：13705180563 <br/><br/>Best regards,<br/>Wang Yuwei 王宇薇<br/>Mobile: +86 13705180563<br/>" +
     *                 "<img src='cid:identifier1234'/>";
     * @param sender 邮件发送器
     * @param to 接受人
     * @param subject 主题
     * @param html 发送内容
     * @param filePath 图片本地地址
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static void sendHtmlMail(JavaMailSenderImpl sender,String to, String subject, String html,String filePath)  {

        try {
            MimeMessage mimeMessage = sender.createMimeMessage();
            // 设置utf-8或GBK编码，否则邮件会有乱码
            MimeMessageHelper messageHelper = null;
            messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setFrom(EMAILFORM, "");
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setText(html, true);
            if (filePath!=null) {
                FileSystemResource res = new FileSystemResource(new File(filePath));
                messageHelper.addInline("identifier1234", res);
            }
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
